$(document).ready(function()
    {
        $.notif_angsuran = function(data)
        {
            $(".currency").toNumber();
            $("#angsuran_table tbody").html('');
            $("#angsuran_table tfoot").html('');

            if(data.data != null)
            {
                var total = 0;
                for(var i = 0; i < data.data.length; i++)
                {
                    var current = data.data[i];
                    $("#angsuran_table tbody").append("<tr>\n\
                                        <td>" + current.cicilan_ke + "</td>\n\
                                        <td class='currency'>" + current.cicilan_pokok + "</td>\n\
                                        <td class='currency'>" + current.margin + "</td>\n\
                                        <td class='currency'>" + current.angsuran + "</td>\n\
                                        <td class='currency'>" + current.sisa_hutang + "</td>\n\
                                        </tr>");
                    total += current.cicilan_pokok + current.besar_bunga;
                }
                $("#angsuran_table tfoot").html("<tr>\n\
                                        <th colspan='3'>Total</th>\n\
                                        <th class='currency'>"+total+"</th>\n\
                                        <th></th>\n\
                                        </tr>");
            }
            $(".currency").formatCurrency();
        }
        
        $(".currency").toNumber();
        $.data_ajax('br-03/peminjaman/generate_new_skema', "#FORM_PINJAMAN", $.notif_angsuran);

        $.print_konsul = function()
        {
            window.location = base_url + "br-03/peminjaman/konsultasi/" + $("#ID_PINJAMAN").val();
        }

        $.print_perjanjian = function()
        {
            window.location = base_url + "br-03/peminjaman/report/" + $("#ID_PINJAMAN").val();
        }
        
        $("#FORM_PINJAMAN").submit(function(){
            return false;
        });
        $("#SELECT_BUKTI").submit(function(){
            return false;
        });

        $('#app_dialog').dialog({
            title: 'Pilih BKK/BKB',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $.refresh = function(data)
        {
            window.location = base_url + "br-03/peminjaman/list_new";
        }

        $.approve = function()
        {
            $.data_ajax('br-03/peminjaman/approve/' + $("#ID_PINJAMAN").val(), "#SELECT_BUKTI", $.refresh);
        }

        $.open_app_dialog = function()
        {
            $('#app_dialog').dialog('open');
        }

        $.reject = function()
        {
            $.data_ajax('br-03/peminjaman/reject/' + $("#ID_PINJAMAN").val(), null, $.refresh);
        }

        $.notif_change_layanan = function()
        {
            $(".currency").formatCurrency();
        }
        
        $.change_layanan = function()
        {
            $(".currency").toNumber();
            $.data_ajax('br-03/peminjaman/change_layanan', "#FORM_PINJAMAN", $.notif_change_layanan);
        }


        $("#CHANGE").click($.change_layanan);
        $("#YES").click($.open_app_dialog);
        $("#NO").click($.reject);
        $("#SUBMIT").click($.approve);

        $("#CETAK_KONSUL").click($.print_konsul);
        $("#CETAK_PERJANJIAN").click($.print_perjanjian);

    });